package com.cloudweb.oa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloudweb.oa.cache.UserCache;
import com.cloudweb.oa.entity.FlowUserAuthority;
import com.cloudweb.oa.entity.User;
import com.cloudweb.oa.mapper.FlowUserAuthorityMapper;
import com.cloudweb.oa.service.IFlowUserAuthorityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudweb.oa.service.IGroupService;
import com.cloudweb.oa.service.IRoleService;
import com.cloudweb.oa.utils.SpringUtil;
import com.cloudwebsoft.framework.util.LogUtil;
import com.redmoon.oa.flow.Leaf;
import com.redmoon.oa.flow.LeafPriv;
import com.redmoon.oa.pvg.PrivDb;
import com.redmoon.oa.pvg.PrivMgr;
import com.redmoon.oa.pvg.Privilege;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fgf
 * @since 2024-01-11
 */
@Service
public class FlowUserAuthorityServiceImpl extends ServiceImpl<FlowUserAuthorityMapper, FlowUserAuthority> implements IFlowUserAuthorityService {
    @Autowired
    FlowUserAuthorityMapper flowUserAuthorityMapper;

    @Lazy
    @Autowired
    IRoleService roleService;

    @Autowired
    UserCache userCache;

    @Autowired
    IGroupService groupService;

    @Async("threadPoolTaskExecutor")
    @Override
    public void refreshFlowUserAuthorityAsync(String userName) {
        // 清除用户的权限
        QueryWrapper<FlowUserAuthority> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        flowUserAuthorityMapper.delete(queryWrapper);

        Leaf lf = new Leaf();
        lf = lf.getLeaf(Leaf.CODE_ROOT);
        Vector<Leaf> v = new Vector<>();
        lf.getAllChild(v, lf);
        for (Object o : v) {
            lf = (Leaf) o;
            LeafPriv leafPriv = new LeafPriv(lf.getCode());
            FlowUserAuthority flowUserAuthority = new FlowUserAuthority();
            if (leafPriv.canUserUrge(userName)) {
                flowUserAuthority.setUserName(userName);
                flowUserAuthority.setTypeCode(lf.getCode());
                flowUserAuthority.setAuthority(LeafPriv.AUTHORITY_URGE);
                flowUserAuthorityMapper.insert(flowUserAuthority);
            }
        }

        // 刷新缓存中赋予用户的权限，特别要注意当创建新用户时，因为异步调用本方法的原因，数据库中尚未插入用户记录，调用本方法会报错
        // userAuthorityCache.refreshUserAuthorities(userName);
    }

    @Override
    public void refreshFlowUserAuthorityByLeaf(String typeCode, String name, int type) {
        List<User> list = null;
        if (type == LeafPriv.TYPE_ROLE) {
            list = roleService.getAllUserOfRole(name, true);
        } else if (type == LeafPriv.TYPE_USERGROUP) {
            list = groupService.getAllUserOfGroup(name);
        } else {
            User user = userCache.getUser(name);
            if (user != null) {
                list = new ArrayList<>();
                list.add(user);
            } else {
                LogUtil.getLog(getClass()).warn("LeafPriv: user: " + name + " is not exist");
            }
        }
        if (list != null) {
            for (User user : list) {
                refreshFlowUserAuthorityByTypeCode(user.getName(), typeCode);
            }
        }
    }

    public void refreshFlowUserAuthorityByTypeCode(String userName, String typeCode) {
        // 清除用户的权限，以免重复
        QueryWrapper<FlowUserAuthority> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        queryWrapper.eq("type_code", typeCode);
        flowUserAuthorityMapper.delete(queryWrapper);

        LeafPriv leafPriv = new LeafPriv(typeCode);
        FlowUserAuthority flowUserAuthority = new FlowUserAuthority();
        if (leafPriv.canUserUrge(userName)) {
            flowUserAuthority.setUserName(userName);
            flowUserAuthority.setAuthority(LeafPriv.AUTHORITY_URGE);
            flowUserAuthority.setTypeCode(typeCode);
            flowUserAuthorityMapper.insert(flowUserAuthority);
        }

        // 刷新缓存中赋予用户的权限
        // userAuthorityCache.refreshUserAuthorities(userName);
    }
}
